#!/bin/sh
#
# see: dh_installdeb(1)

set -e

package="ti-pru-cgt-installer"

#8474382a9880a97c16c41863672840d2aaddb735ef05c933283fa55be6849f6c  ti_cgt_pru_2.1.0_armlinuxa8hf_busybox_installer.sh
#819a4680296fabf1ae0d4e0184276f8ab5324621d35388026d1b678ea71040c9  ti_cgt_pru_2.1.1_armlinuxa8hf_busybox_installer.sh
#15c8d7b9c5a7e8a715060cb2bbce412f0cfd6fa6864a6cb354f6828d309a07ec  ti_cgt_pru_2.1.2_armlinuxa8hf_busybox_installer.sh
#a7a53e250eee2cf9ef180e8a8537a097ffa03ba365a388bb0191399431f85796  ti_cgt_pru_2.1.3_armlinuxa8hf_busybox_installer.sh
#c125bbe4e805b830e5d746ade929dc1f202f25a6863d5005ed97cc5a82e26754  ti_cgt_pru_2.1.4_armlinuxa8hf_busybox_installer.sh
#f2711478f9160a73660a2b2f316ad446bc481141bb9889f1001441beccce3584  ti_cgt_pru_2.1.5_armlinuxa8hf_busybox_installer.sh

version="2.1.5"
FILENAME="ti_cgt_pru_${version}_armlinuxa8hf_busybox_installer.sh"
SHA256SUM_TGZ="f2711478f9160a73660a2b2f316ad446bc481141bb9889f1001441beccce3584"
PARTNER_URL="http://downloads.ti.com/codegen/esd/cgt_public_sw/PRU/${version}"

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

dl_file() {
	wget --no-check-certificate $PARTNER_URL/$FILENAME
}

validdate_file() {
	sha256compare=$(sha256sum $FILENAME | awk '{print $1}')
	if [ ! "x${sha256compare}" = "x${SHA256SUM_TGZ}" ] ; then
		rm -rf $FILENAME
		dl_file
		sha256compare=$(sha256sum $FILENAME | awk '{print $1}')
		if [ ! "x${sha256compare}" = "x${SHA256SUM_TGZ}" ] ; then
			echo "dl failure"
			exit 1
		fi
	fi
}

dl_install() {
	if [ ! -d /var/cache/${package} ] ; then
		mkdir -p /var/cache/${package}
	fi

	cd /var/cache/${package}/

	if [ ! -f $FILENAME ] ; then
		dl_file
	fi

	if [ -f $FILENAME ] ; then
		validdate_file

		chmod +x $FILENAME
		./$FILENAME --prefix /
	fi
}

case "$1" in
    configure)
	dl_install
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

